<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <title>找回密码</title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <style>
    body {
      background-color: #f5f5f5;
      color: #333;
      font-family: Arial, sans-serif;
    }

    .form {
      max-width: 400px;
      margin: 50px auto;
      background-color: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .form-inner {
      display: flex;
      flex-direction: column;
    }

    .input-wrapper {
      margin-bottom: 15px;
    }

    .btn-group {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  </style>
</head>

<body>
  <div id="app">
    <el-form :model="form" :rules="rules" ref="retrieveForm" label-width="80px" class="form">
      <div class="form-inner">
        <h2>找回密码</h2>
        <el-form-item label="账户" prop="account">
          <el-input v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button :disabled="isButtonDisabled" :loading="isLoading" @click="getVerificationCode">
            {{ buttonText }}
          </el-button>
        </el-form-item>
        <el-form-item label="验证码" prop="captcha">
          <el-input v-model="form.captcha"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword"></el-input>
        </el-form-item>
        <div class="btn-group">
          <el-button type="primary" @click="submitForm">重置密码</el-button>
          <a class="btn--text" href="login.html">返回登录</a>
        </div>
      </div>
    </el-form>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="../js/axios-0.18.0.js"></script>
  <script>
    new Vue({
      el: '#app',
      data() {
        const validateConfirmPassword = (rule, value, callback) => {
          if (value === '') {
            callback(new Error('请输入确认密码'));
          } else if (value !== this.form.password) {
            callback(new Error('两次输入的密码不一致'));
          } else {
            callback();
          }
        };

        const validateAccount = (rule, value, callback) => {
          if (value.includes('@')) {
            callback(new Error('用户名不能包含@符号'));
          } else if (value.length < 4 || value.length > 11) {
            callback(new Error('用户名必须为4到11位'));
          } else if (/\s/.test(value)) {
            callback(new Error('用户名不能包含空格'));
          } else {
            callback();
          }
        };

        return {
          form: {
            account: '',
            email: '',
            password: '',
            confirmPassword: '',
            captcha: ''
          },
          rules: {
            account: [
              { required: true, message: '请输入账户', trigger: 'blur' },
              { validator: validateAccount, trigger: 'blur' }
            ],
            email: [
              { required: true, message: '请输入邮箱', trigger: 'blur' },
              { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
            ],
            password: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
            confirmPassword: [
              { required: true, message: '请输入确认密码', trigger: 'blur' },
              { validator: validateConfirmPassword, trigger: 'blur' }
            ],
            captcha: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
          },
          isButtonDisabled: false,
          buttonText: '获取验证码',
          timer: null,
          isLoading: false
        }
      },
      methods: {
        getVerificationCode() {
          if (this.form.email) {
            this.isLoading = true;
            // 发送axios post请求
            axios.post('/com/captcha/retrieve?email=' + this.form.email+'&account='+this.form.account)
              .then(response => {
                if (response.data.success) {
                  this.isLoading = false;
                  this.isButtonDisabled = true;
                  this.buttonText = '60秒后重试';
                  let countdown = 60;
                  this.timer = setInterval(() => {
                    countdown--;
                    this.buttonText = `${countdown}秒后重试`;
                    if (countdown <= 0) {
                      clearInterval(this.timer);
                      this.isButtonDisabled = false;
                      this.buttonText = '获取验证码';
                    }
                  }, 1000);
                  this.$message({
                    message: "已发送验证码",
                    type: 'success'
                  });
                } else {
                  this.isLoading = false;
                  this.$message({
                    message: response.data.msg,
                    type: 'error'
                  });
                }
              })
              .catch(error => {
                this.isLoading = false;
                console.error('获取验证码失败:', error);
              });
            return;
          }
          this.$message({
            message: '请输入邮箱',
            type: 'warning'
          });
        },
        submitForm() {
          this.$refs.retrieveForm.validate((valid) => {
            if (valid) {
              axios.post('/register/retrieve', this.form)
                .then(response => {
                  if (response.data.success) {
                    this.$message({
                      message: "密码重置成功",
                      type: 'success'
                    });
                    window.location.href = 'login.html';
                  } else {
                    this.$message({
                      message: response.data.msg,
                      type: 'error'
                    });
                  }
                })
                .catch(error => {
                  this.$message({
                    message: '重置密码失败，请重试！',
                    type: 'error'
                  });
                  console.error('重置密码失败:', error);
                });
            } else {
              console.log('表单验证失败');
              return false;
            }
          });
        }
      }
    });
  </script>
</body>

</html>